package com.zbkj.crmeb.merchant.admin.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonApiResult;
import com.crmeb.core.common.CommonResult;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.pdd.request.PddAccountRequest;
import com.zbkj.crmeb.pdd.response.PddAccountResponse;
import com.zbkj.crmeb.pdd.service.PddAccountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;


/**
 * 拼多多对应相关账号页面
 * @author daigb
 * @date:  2022-11-14 14:33
 */
@Slf4j
@RestController
@RequestMapping("api/admin/mer/pdd/account")
@Api(tags = "拼多多对应相关账号页面")
public class MerPddAccountController extends AdminBaseController {
	@Autowired
    private PddAccountService pddAccountService;

    /**
     * 分页列表
     * @author daigb
     * @date:  2022-11-14 14:33
     */
    @ApiOperation(value = "分页列表")
    @GetMapping
    public CommonResult<PageInfo<PddAccountResponse>> page(@Validated PageParamRequest pageParamRequest, HttpServletRequest request){
    	return CommonResult.success(pddAccountService.pageEntity(pageParamRequest, getMerId(request)));
    }
    
    /**
     * 新增/修改
     * @author daigb
     * @date:  2022-11-18 09:33
     */
    @ApiOperation(value = "新增/修改")
    @PostMapping
    public CommonResult<Object> save(@Validated @RequestBody PddAccountRequest pddAccountRequest, HttpServletRequest request){
    	return CommonResult.success(pddAccountService.saveOrUpdateEntity(pddAccountRequest, getMerId(request)));
    }
    
    /**
     * 删除
     * @author daigb
     * @date:  2022-11-18 09:33
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, HttpServletRequest request){
    	return CommonResult.success(pddAccountService.delEntity(id, getMerId(request)));
    }
    
    /**
     * 该接口需要验证登陆权限(回调和应用市场进入)
     * @author daigb
     * @date:  2022-11-15 15:52
     */
    @ApiOperation(value = "拼多多授权回调")
    @RequestMapping(value = "/notifyPddAuthToken", method = RequestMethod.GET)
    public CommonApiResult<Object> notifyAccount(HttpServletRequest request) throws Exception{
    	String url = request.getRequestURL().toString() + "?" + request.getQueryString();
		log.info("--------------- 收到拼多多授权回调请求url: {} ---------------", url);
		String code = request.getParameter("code");
		String state = request.getParameter("state");
		if(StringUtils.isBlank(code) || StringUtils.isBlank(state)) {
			log.info("--------------- 拼多多授权回调参数缺失: code={}, state={} ---------------", code, state);
	        return CommonApiResult.success("转到商户列表");
		}
		
		// 回调处理
		boolean b = pddAccountService.notifyAccount(state, code);
		if(b) {
			return CommonApiResult.success();
		} else {
	        return CommonApiResult.failed();
		}
    }
    
}



